package com.db.compare.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.db.compare.exp.RbacAuthExp;
import com.db.compare.model.RbacAuth;
import com.db.compare.mapper.RbacAuthMapper;
import com.db.compare.service.RbacAuthService;
import com.db.compare.vo.RbacAuthVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class RbacAuthServiceImpl implements RbacAuthService {
    @Autowired
    private RbacAuthMapper rbacauthMapper;
    @Override
    public PageInfo<RbacAuth> pageInfo(RbacAuthVo rbacauthVo) {
        PageHelper.startPage(rbacauthVo.getPageNum(),rbacauthVo.getPageSize());
        List<RbacAuth> rbacauthList = rbacauthMapper.findList(rbacauthVo);
        PageInfo<RbacAuth> pageInfo = new PageInfo<>(rbacauthList);
        return pageInfo;
    }

    @Override
    public void download(String ids, HttpServletResponse response)throws Exception{

        String fileName = "权限表";
        try {
            ServletOutputStream outputStream=Appcontext.downloadResponse(response,fileName);;
            if(StringUtils.isBlank(ids)){
                EasyExcel.write(outputStream, RbacAuthExp.class).sheet(fileName).doWrite(null);
            }else{
                String[] arrays = ids.split(",");
                List<RbacAuth> rbacauthList = rbacauthMapper.findByIds(arrays);
                List<RbacAuthExp> rbacauthExpList = new ArrayList<>();
                for(RbacAuth rbacauth:rbacauthList){
                    RbacAuthExp rbacauthExp = new RbacAuthExp();
                    BeanUtils.copyProperties(rbacauth,rbacauthExp);
                    rbacauthExpList.add(rbacauthExp);
                }
                EasyExcel.write(outputStream, RbacAuthExp.class).sheet(fileName).doWrite(rbacauthExpList);
            }
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean upload(MultipartFile file)  {
        ImportParams importParams = new ImportParams();
        try {
            ExcelImportResult<RbacAuthExp> result = ExcelImportUtil.importExcelMore(file.getInputStream(), RbacAuthExp.class,
                    importParams);
            List<RbacAuthExp> rbacauthExps = result.getList();
            for (RbacAuthExp rbacauthExp : rbacauthExps) {
                RbacAuth rbacauth = new RbacAuth();
                BeanUtils.copyProperties(rbacauth,rbacauthExp);
                int i = rbacauthMapper.insert(rbacauth);
                if(i < 1){
                    return false;
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
